package com.hrm.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hrm.config.JSPPage;
import com.hrm.config.MsgType;
import com.hrm.entity.Action;
import com.hrm.entity.Menu;
import com.hrm.entity.Message;
import com.hrm.service.MenuService;
import com.hrm.service.UserLogService;
import com.hrm.util.JSONUtil;

@Controller
@RequestMapping("/menu.do")
public class MenuAction {
	private static final Log log = LogFactory.getLog(MenuAction.class);
	@Autowired
	private MenuService menuService;
	@Autowired
	private UserLogService userLogService;

	public UserLogService getUserLogService() {
		return userLogService;
	}

	public void setUserLogService(UserLogService userLogService) {
		this.userLogService = userLogService;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}


	@RequestMapping(params = "method=getMenusByRoleId")
	public String getMenusByRoleId(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		long roleId = Long.parseLong(request.getParameter("id"));
		List<Menu> menus = menuService.getMenusByRoleId(roleId);
		if (menus != null) {
			String result = JSONUtil.parseList(menus);
			try {
				response.getWriter().print(result);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				Message message = new Message("ERROR", "获取数据失败或数据为空!",
						new Date());
				System.out.println(message.toJSONString());
				response.getWriter().print(message.toJSONString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}
	@RequestMapping(params = "method=getMenus")
	public String getMenus(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		List<Menu> menus = menuService.getMenus();
		if (menus != null) {
			String result = JSONUtil.parseList(menus);
			try {
				response.getWriter().print(result);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				Message message = new Message("ERROR", "获取数据失败或数据为空!",
						new Date());
				System.out.println(message.toJSONString());
				response.getWriter().print(message.toJSONString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
		
	}



	@RequestMapping(params = "method=updateMenu")
	public String updateMenu(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		boolean isAction=request.getParameter("menuId")==null?true:false;
		if(isAction){
			long actionId = Long.parseLong(request.getParameter("actionId"));
			Action action = menuService.getActionById(actionId);
			List<Menu> menus = menuService.getMenus();
			if (action == null) {
				Message message = new Message();
				message.setMsg("获取菜单信息失败!");
				message.setStatus("ERROR");
				message.setTimestamp(new Date());
				try {
					response.getWriter().print(message.toJSONString());
					return null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			map.put("action", action);
			map.put("menus", menus);
			return JSPPage.ACTION_EDIT;
		}else {
			long menuId = Long.parseLong(request.getParameter("menuId"));
			Menu menu = menuService.getMenuById(menuId);
			if (menu == null) {
				Message message = new Message();
				message.setMsg("获取菜单信息失败!");
				message.setStatus("ERROR");
				message.setTimestamp(new Date());
				try {
					response.getWriter().print(message.toJSONString());
					return null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			map.put("menu", menu);
			return JSPPage.MENU_EDIT;
		}
		
		
	}
	@RequestMapping(params = "method=updateActionById")
	public String updateActionById(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
			long id = Long.parseLong(request.getParameter("id"));
			Action action = menuService.getActionById(id);
			String actionName = request.getParameter("actionName") == null ? ""
					: request.getParameter("actionName");
			String icon = request.getParameter("icon") == null ? ""
					: request.getParameter("icon");
			String actionCode = request.getParameter("actionCode") == null ? ""
					: request.getParameter("actionCode");
			String path = request.getParameter("path") == null ? ""
					: request.getParameter("path");
			String remark = request.getParameter("remark") == null ? ""
					: request.getParameter("remark");
			int menuId = request.getParameter("menuId") == null ? -1
					: Integer.parseInt(request.getParameter("menuId"));
			String viewMode = request.getParameter("viewMode") == null ? ""
					: request.getParameter("viewMode");
			action.setActionCode(actionCode);
			action.setActionName(actionName);
			action.setIcon(icon);
			action.setPath(path);
			action.setRemark(remark);
			action.setMenuId(menuId);
			action.setViewMode(viewMode);
			Message message = new Message();
			if (menuService.updateAction(action)<1) {
				message.setMsg("获取菜单信息失败!");
				message.setStatus("ERROR");
				message.setTimestamp(new Date());
				try {
					response.getWriter().print(message.toJSONString());
					return null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else{
				message.setMsg("编辑菜单信息成功!");
				message.setStatus("SUCCESS");
				message.setTimestamp(new Date());
				ActionUtil.insertLog(request, MsgType.TYPE_ACTION, "编辑二级菜单:"+action.getActionName(),userLogService);
				try {
					response.getWriter().print(message.toJSONString());
					return null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return null;
		
	}




	@RequestMapping(params = "method=deleteActionById")
	public String deleteActionById(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		long id = Long.parseLong(request.getParameter("id") == null ? "-1"
				: request.getParameter("id"));
		if (id == -1) {
			Message message = new Message();
			message.setMsg("必须传入菜单ID才能删除!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Message message = new Message();
		if (menuService.deleteActionById(id) < 1) {
			message.setMsg("网络忙，请稍后再试!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			message.setMsg("删除菜单成功!");
			message.setStatus("SUCCESS");
			message.setTimestamp(new Date());
			ActionUtil.insertLog(request, MsgType.TYPE_ACTION, "删除二级菜单:菜单ID为"+id,userLogService);
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}
	@RequestMapping(params = "method=hideActionById")
	public String hideActionById(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		long id = Long.parseLong(request.getParameter("id") == null ? "-1"
				: request.getParameter("id"));
		if (id == -1) {
			Message message = new Message();
			message.setMsg("必须传入菜单ID才能隐藏!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Action action = menuService.getActionById(id);
		action.setViewMode("N");
		Message message = new Message();
		if (menuService.updateAction(action) < 1) {
			message.setMsg("网络忙，请稍后再试!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			message.setMsg("隐藏菜单成功!");
			message.setStatus("SUCCESS");
			message.setTimestamp(new Date());
			ActionUtil.insertLog(request, MsgType.TYPE_ACTION, "隐藏二级菜单:"+action.getActionName(),userLogService);
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
		
	}
	@RequestMapping(params = "method=showActionById")
	public String showActionById(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		long id = Long.parseLong(request.getParameter("id") == null ? "-1"
				: request.getParameter("id"));
		if (id == -1) {
			Message message = new Message();
			message.setMsg("必须传入菜单ID才能显示!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Action action = menuService.getActionById(id);
		action.setViewMode("Y");
		Message message = new Message();
		if (menuService.updateAction(action) < 1) {
			message.setMsg("网络忙，请稍后再试!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			message.setMsg("隐藏菜单成功!");
			message.setStatus("SUCCESS");
			message.setTimestamp(new Date());
			ActionUtil.insertLog(request, MsgType.TYPE_ACTION, "显示二级菜单:"+action.getActionName(),userLogService);
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
		
	}
	@RequestMapping(params = "method=hideMenuById")
	public String hideMenuById(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		long id = Long.parseLong(request.getParameter("id") == null ? "-1"
				: request.getParameter("id"));
		if (id == -1) {
			Message message = new Message();
			message.setMsg("必须传入菜单ID才能隐藏!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Message message = new Message();
		if (menuService.hideMenuById(id) < 1) {
			message.setMsg("网络忙，请稍后再试!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			message.setMsg("隐藏菜单成功!");
			message.setStatus("SUCCESS");
			message.setTimestamp(new Date());
			ActionUtil.insertLog(request, MsgType.TYPE_ACTION, "隐藏一级菜单:一级菜单ID为"+id,userLogService);
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
		
	}
	@RequestMapping(params = "method=showMenuById")
	public String showMenuById(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		long id = Long.parseLong(request.getParameter("id") == null ? "-1"
				: request.getParameter("id"));
		if (id == -1) {
			Message message = new Message();
			message.setMsg("必须传入菜单ID才能删除显示!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Message message = new Message();
		if (menuService.showMenuById(id) < 1) {
			message.setMsg("网络忙，请稍后再试!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			message.setMsg("显示菜单成功!");
			message.setStatus("SUCCESS");
			message.setTimestamp(new Date());
			ActionUtil.insertLog(request, MsgType.TYPE_ACTION, "显示一级菜单:一级菜单ID为"+id,userLogService);
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
		
	}


	@RequestMapping(params = "method=addAction")
	public String addAction(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		log.info("-----" + request.getParameter("actionName"));
		String actionName = request.getParameter("actionName") == null ? ""
				: request.getParameter("actionName");
		String icon = request.getParameter("icon") == null ? ""
				: request.getParameter("icon");
		String actionCode = request.getParameter("actionCode") == null ? ""
				: request.getParameter("actionCode");
		String path = request.getParameter("path") == null ? ""
				: request.getParameter("path");
		String remark = request.getParameter("remark") == null ? ""
				: request.getParameter("remark");
		if ("".equals(actionName) || actionName.getBytes().length >= 50) {
			Message message = new Message();
			message.setMsg("菜单名称不能为空且长度必须在50个字节内!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if ("".equals(actionName) ||actionCode.getBytes().length >= 50) {
			Message message = new Message();
			message.setMsg("菜单编码不能为空且长度不能超过50个字节!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if ("".equals(path) ||path.getBytes().length >= 50) {
			Message message = new Message();
			message.setMsg("URL不能为空且长度不能超过50个字节!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if ("".equals(path) ||remark.getBytes().length >= 100) {
			Message message = new Message();
			message.setMsg("备注长度不能超过100个字节!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		int menuId=Integer.parseInt(request.getParameter("menuId")==null?"1":request.getParameter("menuId"));
		String viewMode=request.getParameter("viewMode")==null?"Y":request.getParameter("viewMode");
		Action action = new Action();
		action.setMenuId(menuId);
		action.setPath(path);
		action.setIcon(icon);//
		action.setActionCode(actionCode);
		action.setActionName(actionName);
		action.setRefreshTime(new Date());
		action.setViewMode(viewMode);
		action.setRemark(remark);
		Message message = new Message();
		if (menuService.addAction(action) < 1) {
			message.setMsg("网络忙，请稍后再试!");
			message.setStatus("ERROR");
			message.setTimestamp(new Date());
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			message.setMsg("新增菜单成功!");
			message.setStatus("SUCCESS");
			message.setTimestamp(new Date());
			ActionUtil.insertLog(request, MsgType.TYPE_ACTION, "新增二级菜单:"+actionName,userLogService);
			try {
				response.getWriter().print(message.toJSONString());
				return null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}

}
